﻿Public Class ucEditApplication1
	Inherits System.Web.UI.UserControl

	Public Event NextStep()
	Public Event Cancel()
	Public Event Alert(ByVal msg As String)

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
				LoadData()
			End If
		End If
		InitJS()

		OFFICER_APP_ID.Attributes.Add("onBlur", "ChangeFormat(this,0,'.','',false,'text');ChangFormatID();")
		If HttpContext.Current.User.IsInRole("member") Then
			Me.trPassword.Visible = False
			Me.trRePassword.Visible = False
			Me.trRemark.Visible = False
			Me.trStatus.Visible = False
		End If
	End Sub

	Private Sub InitJS()
		Dim sbJS As New Text.StringBuilder
		sbJS.AppendLine("var brithdayID = 'text_" & Me.OFFICER_BIRTHDATE.UniqueID & "';")
		sbJS.AppendLine("var carddayID = 'text_" & Me.OFFICER_CARD_EXPIREDATE.UniqueID & "';")
		sbJS.AppendLine("var appID = '" & Me.OFFICER_APP_ID.ClientID & "';")
		Me.ltrJS.Text = "<script language=""JavaScript"">" & sbJS.ToString & "</script>"
	End Sub

	Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) 'Handles btnNext.Click
		Dim registerInfo As Entity.OfficerInfoItem
		If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
			registerInfo = GlobalApp.DataSourceEditOfficer
		Else
			registerInfo = New Entity.OfficerInfoItem
		End If

		With registerInfo
			.officer_app_id = Me.OFFICER_APP_ID.Text.Replace("-", "")
			.offcier_app_tel = Me.OFFCIER_APP_TEL.Text
			.officer_firstname_tha = Me.OFFICER_FIRSTNAME_THA.Text
			.officer_lastname_tha = Me.OFFICER_LASTNAME_THA.Text
			.officer_firstname_eng = Me.OFFICER_FIRSTNAME_ENG.Text
			.officer_lastname_eng = Me.OFFICER_LASTNAME_ENG.Text
			.officer_sex = Me.OFFICER_SEX.SelectedValue
			.officer_card_no = Me.OFFICER_CARD_NO.Text

			If Common.UtilityDateTime.ConvertDate(Me.OFFICER_BIRTHDATE.Text).HasValue Then
				.officer_birthdate = Common.UtilityDateTime.ConvertDate(Me.OFFICER_BIRTHDATE.Text).Value.AddYears(-543)
			Else
				RaiseEvent Alert("กรุณาระบุวันเกิดตามบัตรให้ถูกรูปแบบ")
				Exit Sub
			End If

			'.officer_birthdate = New Date(Me.OFFICER_BIRTHDATE.Year, Me.OFFICER_BIRTHDATE.Month, Me.OFFICER_BIRTHDATE.Day).AddYears(-543)

			Try
				.officer_card_expiredate = New Date(Me.OFFICER_CARD_EXPIREDATE.Year, Me.OFFICER_CARD_EXPIREDATE.Month, Me.OFFICER_CARD_EXPIREDATE.Day).AddYears(-543)
			Catch
			End Try
			.officer_card_expiredate_type = Me.OFFICER_CARD_EXPIREDATE_TYPE.SelectedValue
			.officer_married_status = Me.OFFICER_MARRIED_STATUS.SelectedValue
			If Me.chbChangeStatus.Checked Then
				.officer_status = "WN"
			Else
				.officer_status = Nothing
			End If
			.officer_remark = Me.OFFICER_REMARK.Text
            If GlobalApp.Authen IsNot Nothing Then
                .create_user_id = GlobalApp.Authen.offcier_id
                .create_date = Now
                .upd_user_id = GlobalApp.Authen.offcier_id
                .upd_date = Now
            End If
		End With

		If Math.Round((DateTime.Today - registerInfo.officer_birthdate.Value).TotalDays / 365) < 18 Then
			RaiseEvent Alert("คุณสมบัติของผู้สมัครต้องมีอายุมากกว่า 18 ปี")
			Exit Sub
		End If

		GlobalApp.DataSourceEditOfficer = registerInfo

		'change password
		If Not String.IsNullOrEmpty(Me.txtPassword.Text) Then
			Dim pwd As New Entity.UserInfoItem
			pwd.user_id = registerInfo.officer_app_id
			pwd.upd_user_id = GlobalApp.Authen.offcier_id
			pwd.user_password = Me.txtPassword.Text
			pwd.offcier_id = registerInfo.officer_id
			GlobalApp.DataSourceEditPWD = pwd
		End If

		RaiseEvent NextStep()
		Response.Redirect("EditApplication1.aspx", False)
	End Sub

	Private Sub LoadData()

		Dim registerInfo As Entity.OfficerInfoItem
		registerInfo = GlobalApp.DataSourceEditOfficer
		With registerInfo
			Try
				Me.OFFICER_APP_ID.Text = Common.Utility.FormatAppId(.officer_app_id)
			Catch
				Me.OFFICER_APP_ID.Text = .officer_app_id
			End Try

			Me.OFFCIER_APP_TEL.Text = .offcier_app_tel
			Me.OFFICER_FIRSTNAME_THA.Text = .officer_firstname_tha
			Me.OFFICER_LASTNAME_THA.Text = .officer_lastname_tha
			Me.OFFICER_FIRSTNAME_ENG.Text = .officer_firstname_eng
			Me.OFFICER_LASTNAME_ENG.Text = .officer_lastname_eng
			Common.Utility.SelectChoice(Me.OFFICER_SEX, .officer_sex)
			Me.OFFICER_CARD_NO.Text = .officer_card_no
			'Me.OFFICER_BIRTHDATE.value = .officer_birthdate.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
			Try
				Me.OFFICER_CARD_EXPIREDATE.value = .officer_card_expiredate.Value.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
			Catch
			End Try
			Common.Utility.SelectChoice(Me.OFFICER_CARD_EXPIREDATE_TYPE, .officer_card_expiredate_type)
			Common.Utility.SelectChoice(Me.OFFICER_MARRIED_STATUS, .officer_married_status)
			Common.Utility.SelectChoice(Me.ddlStatus, .officer_status)
			Me.txtStatus.Text = Common.Utility.ConvertStatus(.officer_status)
			Me.OFFICER_REMARK.Text = .officer_remark

			Select Case .officer_status
				Case "WT"
					Me.chbChangeStatus.Text = "ตรวจสอบเอกสาร"
					Me.chbChangeStatus.Visible = True
				Case "WN", "NM", "EX", "EP"
					Me.chbChangeStatus.Text = "ต่ออายุ"
					Me.chbChangeStatus.Visible = True
				Case Else
					Me.chbChangeStatus.Visible = False
			End Select

		End With
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs)	'Handles btnCancel.Click
		If HttpContext.Current.User.IsInRole("admin") Then
			Response.Redirect("OfficerManagement.aspx", False)
		Else
			Response.Redirect("ViewStatusRegister.aspx", False)
		End If
	End Sub
End Class